package net.ebakker.tvseries.reader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.ebakker.tvseries.main.Pair;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonReader {

	public static Map<String, Pair<String, File>> readFile(File json) throws IOException,
			ParseException {
		JSONParser parser = new JSONParser();
		FileReader jsonReader = new FileReader(json);
		JSONObject object = (JSONObject)parser.parse(jsonReader);
		Map<String, Pair<String, File>> seriesMap = new HashMap<>();
		JSONArray series = (JSONArray)object.get("series");
		for (Object serie : series) {
			JSONObject jsonSerie = (JSONObject)serie;
			String serieName = (String)jsonSerie.get("serie");
			String serieFolder = (String)jsonSerie.get("folder");
			String serieType = (String)jsonSerie.get("type");
			Pair<String, File> folderTypePair = new Pair<>(serieType, new File(serieFolder));
			seriesMap.put(serieName, folderTypePair);
		}
		return seriesMap;
	}
}
